package com.nantai.jiafang.nantaijiafang.deviceManager.devicemanager;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.nantai.jiafang.nantaijiafang.R;
import com.nantai.jiafang.nantaijiafang.deviceManager.bean.KeepTask;
import com.nantai.jiafang.nantaijiafang.deviceManager.constant.Constans;
import com.nantai.jiafang.nantaijiafang.deviceManager.http.Error;
import com.nantai.jiafang.nantaijiafang.deviceManager.http.HttpClient;
import com.nantai.jiafang.nantaijiafang.deviceManager.http.ResponseHandler;
import com.nantai.jiafang.nantaijiafang.deviceManager.utils.MessagePop;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;

public class KeepActivity extends AppCompatActivity {

    @InjectView(R.id.tv_back)
    TextView mTvBack;
    @InjectView(R.id.tv_toolbar_title)
    TextView mTvToolbarTitle;
    @InjectView(R.id.image_toolbar)
    TextView mImageToolbar;
    @InjectView(R.id.toolbar_menu)
    TextView mToolbarMenu;
    @InjectView(R.id.iv_toolbar_menu)
    RelativeLayout mIvToolbarMenu;
    @InjectView(R.id.toolbar)
    Toolbar mToolbar;
    @InjectView(R.id.layout_tool_bar)
    LinearLayout mLayoutToolBar;
    @InjectView(R.id.title_tx)
    TextView mTitleTx;
    @InjectView(R.id.code_tx)
    EditText mCodeTx;
    @InjectView(R.id.scan_btn)
    Button mScanBtn;
    @InjectView(R.id.place_tv)
    TextView mPlaceTv;
    @InjectView(R.id.type_tv)
    TextView mTypeTv;
    @InjectView(R.id.keepTime_tv)
    TextView mKeepTimeTv;
    @InjectView(R.id.scrapSelect)
    Button mScrapSelect;
    @InjectView(R.id.scrap_reason_et)
    TextView mScrapReasonEt;
    @InjectView(R.id.scrap_back)
    LinearLayout mScrapBack;
    @InjectView(R.id.action_btn)
    Button mActionBtn;

    private MaterialDialog showDialog = null;
    private String reasonId = null;
    private KeepTask device=null;
    private String selectIndex=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_keep);
        ButterKnife.inject(this);
        mTvToolbarTitle.setText("保养");
        mToolbar.setNavigationIcon(R.drawable.md_nav_back);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        device = (KeepTask) getIntent().getSerializableExtra("device");
        selectIndex =getIntent().getStringExtra("selectIndex");
        refreshUI();
        showDialog = new MaterialDialog.Builder(KeepActivity.this)
                .content("正在保养")
                .progress(true, 0, false)
                .cancelable(false).build();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Intent intent = getIntent();
        String reasonid = intent.getStringExtra("reasonId");
        String content = intent.getStringExtra("content");
        if (content != null) {
            mScrapReasonEt.setText(content);
        }
        if (reasonid != null) {
            reasonId = reasonid;
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (intent != null) {
            setIntent(intent);
        }
    }

    private void  refreshUI()
    {
        if(device!=null)
        {
            mCodeTx.setText(device.getQr_code());
            mPlaceTv.setText("设备位置:"+device.getPlace_name());
            mTypeTv.setText("设备类型:"+device.getType_name());
            mKeepTimeTv.setText("下次保养时间:"+device.getNext_keep_time());
        }
    }


    @OnClick({R.id.scrapSelect, R.id.action_btn,R.id.scan_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.scrapSelect: {
                Intent intent = new Intent(KeepActivity.this, ReasonActivity.class);
                intent.putExtra("reasonType", "2");
                if (!(mCodeTx.getText() == null || mCodeTx.getText().toString().equals(""))) {
                    String deviceType = mCodeTx.getText().toString().substring(0, 3);
                    intent.putExtra("deviceType", deviceType);
                } else {
                    MessagePop.ToastMessage(KeepActivity.this, "请输入设备条码");
                    return;
                }
                startActivity(intent);
            }
            break;
            case R.id.scan_btn:{
                Intent intent = new Intent(KeepActivity.this, CaptureActivity.class);
                intent.putExtra("operate_type", String.valueOf(Constans.KeepCode));
                startActivityForResult(intent, 20);
            }break;
            case R.id.action_btn: {
                if (mCodeTx.getText() == null || mCodeTx.getText().toString().equals("")) {
                    MessagePop.ToastMessage(KeepActivity.this, "请输入设备条码");
                    return;
                }
                if (reasonId == null) {
                    MessagePop.ToastMessage(KeepActivity.this, "请选择保养原因");
                    return;
                }

                showDialog.show();
                HttpClient client = new HttpClient(KeepActivity.this);
                Call call = client.service().keepUp(mCodeTx.getText().toString(),reasonId);
                client.request(call, new ResponseHandler<ResponseBody>() {
                    @Override
                    public void onSuccess(ResponseBody device) {
                        showDialog.dismiss();
                        if(selectIndex!=null)
                        {
                            Intent intent = getIntent();
                            intent.putExtra("selectIndex",selectIndex);
                            KeepActivity.this.setResult(20, intent);
                            KeepActivity.this.finish();
                        }else
                        {
                            MessagePop.ToastMessage(KeepActivity.this, "保养成功");
                        }
                    }

                    @Override
                    public void onFailure(int code, Error e) {
                        showDialog.dismiss();
                        if (code != 404) {
                            MessagePop.ToastMessage(KeepActivity.this, e.getMessage());
                        } else {
                            MessagePop.ToastMessage(KeepActivity.this, "网络异常");
                        }

                    }
                });
            }
            break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //可以根据多个请求代码来作相应的操作
        if (20 == resultCode) {
            device = (KeepTask) data.getSerializableExtra("device");
            refreshUI();
        }
    }
}
